package cn.wang.controller;

import cn.wang.pojo.SpecGroup;
import cn.wang.pojo.SpecParam;
import cn.wang.service.ISpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;
import javax.ws.rs.GET;
import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecificationController {
    @Autowired
    private ISpecificationService specificationService;

    /**
     * 根据商品分类的id查询所有的规格参数分组(不包含分组下的规格参数)
     * @param cid
     * @return
     */
    @GetMapping("/groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupsByCid(@PathVariable("cid") Long cid){
        return ResponseEntity.ok(specificationService.queryGroupsByCid(cid));
    }

    /**
     * 根据商品分类的id查询所有的规格参数分组(包含分组下的规格参数)
     * @param cid
     * @return
     */
    @GetMapping("/groupAll/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupsAllByCid(@PathVariable("cid") Long cid){
        return ResponseEntity.ok(specificationService.queryGroupsAllByCid(cid));
    }

    /**
     * 保存规格分组
     * @param group
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Integer> saveGroup(@RequestBody @Valid SpecGroup group){ // valid 使用实体类中的注解校验
        return ResponseEntity.ok(specificationService.saveGroup(group));
    }

    /**
     * 修改规格分组
     * @param group
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Integer> UpdateGroup(@RequestBody @Valid SpecGroup group){ // valid 使用实体类中的注解校验
        return ResponseEntity.ok(specificationService.UpdateGroup(group));
    }

    /**
     * 根据规格分组id删除
     * @param gid
     * @return
     */
    @DeleteMapping("/group/{gid}")
    public ResponseEntity<Integer> UpdateGroup(@PathVariable Long gid){ // valid 使用实体类中的注解校验
        return ResponseEntity.ok(specificationService.deleteGroupById(gid));
    }

    // 规格参数相关

    /**
     * 根据据cid , gid , searching 进行查询
     * 根据规格分组,查询规格
     * @param gid 规格分组
     * @param cid 商品分类
     * @param searching 是否搜索
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParam>> getParams(
            @RequestParam(name = "gid", required = false) Long gid,
            @RequestParam(name = "cid", required = false) Long cid,
            @RequestParam(name = "searching", required = false) Boolean searching
            ){
        return ResponseEntity.ok(specificationService.queryPrams(gid,cid,searching));
    }

    @PostMapping("param")
    public ResponseEntity<Integer> saveParam(@RequestBody @Valid SpecParam specParam){
        return ResponseEntity.ok(specificationService.saveParam(specParam));
    }
    @DeleteMapping("/param/{pid}")
    public ResponseEntity<Integer> deleteParams(@PathVariable("pid") Long pid){
        return ResponseEntity.ok(specificationService.deleteParamByPid(pid));
    }

    @PutMapping("/param")
    public ResponseEntity<Integer> updateParam(@RequestBody @Valid SpecParam specParam){
        return ResponseEntity.ok(specificationService.updateParam(specParam));
    }

}
